package com.zfl;

import java.util.Arrays;

public class CoinChange {

    public static void main(String[] args) {
        int[] coins = {2};
        int amount = 11;
        int res = new CoinChange().coinChange(coins, amount);
        System.out.println("res = " + res);
    }

    public int coinChange(int[] coins, int amount) {

        int[] dp = new int[amount + 1];

        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;

//        for (int i = 0; i < dp.length; i++) {
//            dp[i] = i;
//        }

//        System.out.println("dp = " + Arrays.toString(dp));

        for (int i = 1; i <= amount; i++) {

            for (int j = 0; j < coins.length; j++) {
                if (i - coins[j] >= 0 && dp[i - coins[i]] != Integer.MAX_VALUE) {
                    dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]);
                }
            }
//            System.out.println("dp = " + Arrays.toString(dp));
        }
        return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
    }
}
